﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Cistern.Modify
{
    public class PresenterModifyCistern
    {
        IModifyBasicCisternView viewModifyBasic;
        Model.Cistern modifyCistern = new Model.Cistern() { Person = new Model.Person() };
        IModifyPrincipalView view;
        public PresenterModifyCistern(IModifyPrincipalView View, Model.Cistern cistern)
        {
            view = View;
            modifyCistern = cistern;        
            
        }


        public void GoStepTwo()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyCistern = view.ModifyBasic.CisternToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyCistern = view.ModifyBasic.CisternToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void ModifyCistern()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyCistern = view.ModifyBasic.CisternToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerCistern.UpdateCistern(modifyCistern);
                view.SystemMessage("La cisterna Fue Modificada Exitosamente.!!!!");
                view.ReturnSearchCistern();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                var mp = new Model.Cistern();
                mp.Capacity = view.ModifyBasic.CisternToModify.Capacity;
                mp.LicencePlate = view.ModifyBasic.CisternToModify.LicencePlate;
                //ManagerCistern.VerifyDataCisternIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        private bool PassSecondStep()
        {

            try
            {
                var mp = new Model.Cistern() { Person = new Model.Person() };
                mp.Person.PersonId = view.ModifyBasic.CisternToModify.Person.PersonId;
                mp.Person.Names = view.ModifyBasic.CisternToModify.Person.Names;
                mp.Person.LastNames = view.ModifyBasic.CisternToModify.Person.LastNames;
                mp.Person.Phone = view.ModifyBasic.CisternToModify.Person.Phone;
  
                ManagerCistern.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewModifyBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewModifyBasic.GoNextPage();
        }

        private void ProcedureAfterCall()
        {

            if (viewModifyBasic != null)
                modifyCistern = viewModifyBasic.CisternToModify;
            viewModifyBasic = view.ModifyBasic;
            viewModifyBasic.CisternToModify = modifyCistern;
            viewModifyBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string messageValue)
        {
            view.ErrorMessage(messageValue);
        }
        public void NotifySystemMessage(string messageValue)
        {
            view.SystemMessage(messageValue);
        }
    }
}
